package com.example.usegson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    String AnimalsJson = "{\n" +
            "\t\"animals\":{\n" +
            "\t\"dog\":[\n" +
            "\t\t{\"name\":\"Rufus\",\"breed\":\"labrador\",\"count\":1,\"twoFeet\":false},\n" +
            "\t\t{\"name\":\"Marty\",\"breed\":\"whippet\",\"count\":1,\"twoFeet\":false}\n" +
            "\t],\n" +
            "\t\"cat\":{\"name\":\"Matilda\"}\n" +
            "}\n" +
            "}";
    String weatherStr = "{\"time\":\"2019-03-25 08:21:33\",\"cityInfo\":{\"city\":\"广州市\",\"cityId\":\"101280101\",\"parent\":\"广东\",\"updateTime\":\"07:39\"},\"date\":\"20190325\",\"message\":\"Success !\",\"status\":200,\"data\":{\"shidu\":\"99%\",\"pm25\":8.0,\"pm10\":12.0,\"quality\":\"优\",\"wendu\":\"13\",\"ganmao\":\"各类人群可自由活动\",\"yesterday\":{\"date\":\"24\",\"sunrise\":\"06:29\",\"high\":\"高温 17.0℃\",\"low\":\"低温 12.0℃\",\"sunset\":\"18:39\",\"aqi\":15.0,\"ymd\":\"2019-03-24\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"中雨\",\"notice\":\"记得随身携带雨伞哦\"},\"forecast\":[{\"date\":\"25\",\"sunrise\":\"06:28\",\"high\":\"高温 19.0℃\",\"low\":\"低温 14.0℃\",\"sunset\":\"18:40\",\"aqi\":18.0,\"ymd\":\"2019-03-25\",\"week\":\"星期一\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"26\",\"sunrise\":\"06:27\",\"high\":\"高温 24.0℃\",\"low\":\"低温 16.0℃\",\"sunset\":\"18:40\",\"aqi\":69.0,\"ymd\":\"2019-03-26\",\"week\":\"星期二\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"27\",\"sunrise\":\"06:26\",\"high\":\"高温 26.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-27\",\"week\":\"星期三\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"28\",\"sunrise\":\"06:25\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:41\",\"aqi\":55.0,\"ymd\":\"2019-03-28\",\"week\":\"星期四\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"29\",\"sunrise\":\"06:24\",\"high\":\"高温 25.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:41\",\"aqi\":52.0,\"ymd\":\"2019-03-29\",\"week\":\"星期五\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"30\",\"sunrise\":\"06:23\",\"high\":\"高温 25.0℃\",\"low\":\"低温 19.0℃\",\"sunset\":\"18:42\",\"aqi\":57.0,\"ymd\":\"2019-03-30\",\"week\":\"星期六\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"31\",\"sunrise\":\"06:22\",\"high\":\"高温 24.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-03-31\",\"week\":\"星期日\",\"fx\":\"无持续风向\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"01\",\"sunrise\":\"06:21\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:42\",\"ymd\":\"2019-04-01\",\"week\":\"星期一\",\"fx\":\"东北风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"02\",\"sunrise\":\"06:20\",\"high\":\"高温 26.0℃\",\"low\":\"低温 18.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-02\",\"week\":\"星期二\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"晴\",\"notice\":\"愿你拥有比阳光明媚的心情\"},{\"date\":\"03\",\"sunrise\":\"06:19\",\"high\":\"高温 27.0℃\",\"low\":\"低温 20.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-03\",\"week\":\"星期三\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"04\",\"sunrise\":\"06:18\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:43\",\"ymd\":\"2019-04-04\",\"week\":\"星期四\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"05\",\"sunrise\":\"06:18\",\"high\":\"高温 31.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-05\",\"week\":\"星期五\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"06\",\"sunrise\":\"06:17\",\"high\":\"高温 30.0℃\",\"low\":\"低温 22.0℃\",\"sunset\":\"18:44\",\"ymd\":\"2019-04-06\",\"week\":\"星期六\",\"fx\":\"南风\",\"fl\":\"<3级\",\"type\":\"多云\",\"notice\":\"阴晴之间，谨防紫外线侵扰\"},{\"date\":\"07\",\"sunrise\":\"06:16\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-07\",\"week\":\"星期日\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"},{\"date\":\"08\",\"sunrise\":\"06:15\",\"high\":\"高温 29.0℃\",\"low\":\"低温 23.0℃\",\"sunset\":\"18:45\",\"ymd\":\"2019-04-08\",\"week\":\"星期一\",\"fx\":\"东南风\",\"fl\":\"<3级\",\"type\":\"小雨\",\"notice\":\"雨虽小，注意保暖别感冒\"}]}}";
    String urlStr = "http://t.weather.sojson.com/api/weather/city/101280101";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.GetWeather).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        useHttpConnection();
                    }
                }).start();
            }
        });

        findViewById(R.id.weatherAndJson).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Weather weather = gson.fromJson(weatherStr, Weather.class);
                String type = weather.getData().getForecast().get(0).getType();
                Log.e(TAG, "type:" + type);
            }
        });

        findViewById(R.id.animalsAndJson).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                Root root = gson.fromJson(AnimalsJson, Root.class);
                String catName = root.getAnimals().getCat().getName();
                Log.e(TAG, "catName:" + catName);
            }
        });

        findViewById(R.id.userAndJson).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();

                //将user转换成json字符串
                User user = new User("张三", 18);
                String userJsonStr = gson.toJson(user);
                Log.e(TAG, "userJsonStr:" + userJsonStr);

                //将json字符串转换成user
                try {
                    JSONObject jsonObject = new JSONObject(userJsonStr);
                    String jsonObjectStr = jsonObject.toString();
                    User userStr = gson.fromJson(jsonObjectStr, User.class);
                    Log.e(TAG, "userStr:" + userStr.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void useHttpConnection() {
        try {
            //创建http链接
            URL url = new URL(urlStr);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            Log.e(TAG, "ResponseCode:" + String.valueOf(httpURLConnection.getResponseCode()));

            readHtmlContent(httpURLConnection);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void readHtmlContent(HttpURLConnection httpURLConnection) throws IOException, JSONException {
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuffer stringBuffer = new StringBuffer();
        String line = bufferedReader.readLine();
        while (line != null) {
            stringBuffer.append(line);
            line = bufferedReader.readLine();
        }
        Log.e(TAG, "readHtmlContent: " + stringBuffer.toString());
        String weatherJsonStr = stringBuffer.toString();
        GetWeatherJson(weatherJsonStr);
    }

    private void GetWeatherJson(String weatherJsonStr) throws JSONException {
        Gson gson = new Gson();
        Weather weather = gson.fromJson(weatherStr, Weather.class);
        String type = weather.getData().getForecast().get(0).getType();
        Log.e(TAG, "GetWeatherJson: " + type);
    }
}
